<?php defined('SYSPATH') OR die('No direct access allowed.');

class Render_Csv extends Render {

	public static function render($response, array $vars=array(), array $globals=array(), $file=FALSE, array $options=array())
	{
		if ( empty($vars['_no_header']) && ($response instanceof Response) ) {
			$response->headers('Content-Type', 'application/vnd.ms-excel; charset='.Kohana::$charset);
		}
		
		$data = isset($vars['_data']) ? $vars['_data'] : NULL;
		$result = csv_encode($data);
		if (!empty($file))
		{
			$filepath = tempnam(Kohana::$cache_dir.DIRECTORY_SEPARATOR.'util', 'phpcsv_');
			file_put_contents($filepath, $result);

			$response->x_send_file($filepath, $file);
			$result = NULL;
		}
		
		return $result;
	}

}


